﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using BlueLotus7.Common;

namespace BlueLotus7.ExchangeCore
{
    public abstract class BaseDistributor : IDistributor
    {
        protected TimeSpan borderTime;

        public DateTime? LastDistributeStamp
        {
            get;
            protected set;
        }

        public DateTime NextDistributeStamp
        {
            get;
            protected set;
        }

        public string SourceIdentity
        {
            get;
            protected set;
        }

        public int Interval
        {
            get;
            protected set;
        }

        public string SourceUrl
        {
            get;
            protected set;
        }

        public BaseDistributor(string sourceUrl, int interval, string sourceIdentity)
        {
            this.SourceUrl = sourceUrl;
            this.Interval = interval;
            this.SourceIdentity = sourceIdentity;
            this.NextDistributeStamp = DateTime.Now;
        }

        public bool IsTimeUp()
        {
            return this.NextDistributeStamp <= DateTime.Now;
        }

        public List<ExchangeItem> GetExchangeItems()
        {
            return GetExchangeItems(null);
        }

        protected List<ExchangeItem> GetExchangeItems(DateTime? date)
        {
            if (date == null)
            {
                date = DateTime.Now;
            }

            if ((date.Value.Hour * 60 + date.Value.Minute) > this.borderTime.TotalMinutes)
            {
                date = date.Value.AddDays(1);
            }

            Dictionary<string, string> parameters = GenerateParameters(date.Value);
            return GetExchangeItems(parameters, date.Value);
        }

        protected abstract Dictionary<string, string> GenerateParameters(DateTime date);

        protected List<ExchangeItem> GetExchangeItems(Dictionary<string, string> parameters, DateTime date)
        {
            try
            {
                string content = GetSourceContent(parameters);
                return ConvertObjects(content, date);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to get objects via " + this.GetType().FullName, ex);
            }
            finally
            {
                this.LastDistributeStamp = DateTime.Now;
                this.NextDistributeStamp = DateTime.Now.AddMinutes(this.Interval);
            }
        }

        protected virtual string GetSourceContent(Dictionary<string, string> parameters)
        {
            string result = string.Empty;

            if (!string.IsNullOrWhiteSpace(this.SourceUrl))
            {
                HttpWebRequest request = WebUtil.Instance.GenerateHttpWebRequestByUrl(this.SourceUrl);
                if (parameters != null && parameters.Keys.Count > 0)
                {
                    WebUtil.Instance.FillPostDataOnHttpWebRequest(request, parameters);
                }
                result = WebUtil.Instance.ReadTextFromResponse(request.GetResponse(), Encoding.UTF8);
            }

            return result;
        }

        protected abstract List<ExchangeItem> ConvertObjects(string content, DateTime date);
    }
}
